-
Notifications
You must be signed in to change notification settings - Fork 53
Feature/fix write method #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/fix write method #145
Conversation
|
@matt-l-w r? |
|
Doesn't look like the Unicode branch will get tested - might be nice to add in a case for that... Otherwise looks good! |
|
|
||
|
|
||
| @pytest.fixture | ||
| def tempdir(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytest provides a handy tmpdir fixture
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++
| try: | ||
| f.write(unicode(json.dumps(data, indent=4))) | ||
| except NameError: | ||
| f.write(json.dumps(data, indent=4)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using json.dumps try json.dump as this returns a file-like object and shouldn't require the try/except.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm running into an error with py27 when using json.dump
TypeError: write() argument 1 must be unicode, not str
def test_write_results_to_file(tmpdir, mocker):
axe = Axe(mocker.MagicMock())
data = {"testKey": "testValue"}
filename = path.join(str(tmpdir), "results.json")
axe.write_results(data, filename)
def write_results(self, data, name="results.json"):
"""
Write JSON to file with the specified name.
:param name: Name of file to be written to.
:param output: JSON object.
"""
with open(name, "w", encoding="utf8") as f:
json.dump(data, f)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, sorry - that sucks. You could consider using futures or six to make this code look a little nicer, but I'm not sure if it's worth the additional dependency.
| try: | ||
| from unittest.mock import MagicMock | ||
| except ImportError: | ||
| from mock import MagicMock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to consider https://github.com/pytest-dev/pytest-mock/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
building off of #144 by @matt-l-w
fixed mock import error